﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml.Lite.Core.Office;
using P = DocumentFormat.OpenXml.Lite.PresentationML.Presentation.Base;
using DocumentFormat.OpenXml.Lite.PresentationML.Presentation.PresentationElements;
using DocumentFormat.OpenXml.Lite.PresentationML.Presentation.Types;
using DocumentFormat.OpenXml.Lite.PresentationML.Presentation.SlideElements;
using DocumentFormat.OpenXml.Lite.PresentationML.Drawing;
using DMLC = DocumentFormat.OpenXml.Lite.DrawingML.Color;
using DMLE = DocumentFormat.OpenXml.Lite.DrawingML.Effect;
using DMLF = DocumentFormat.OpenXml.Lite.DrawingML.Font;
using DMLG = DocumentFormat.OpenXml.Lite.DrawingML.Geometry;
using DMLGP = DocumentFormat.OpenXml.Lite.DrawingML.Geometry.Properties;
using DMLTx = DocumentFormat.OpenXml.Lite.DrawingML.Text;
using DMLTf = DocumentFormat.OpenXml.Lite.DrawingML.Transformation;
using DMLT = DocumentFormat.OpenXml.Lite.DrawingML.Types;
using DMLS = DocumentFormat.OpenXml.Lite.DrawingML.Style;
using DMLV = DocumentFormat.OpenXml.Lite.DrawingML.Visual;

namespace Samples.Presentation
{
    class Figure
    {
        public static void Create()
        {
            P.Presentation pres = DocumentManager.Instance.CreatePresentation();
            pres.SlideSize = new SlideSize()
            {
                ExtentLength = 9144000,
                ExtentWidth = 6858000,
                Type = ST_SlideSize.Screen_4x3
            };
            pres.NoteSize = new NoteSize()
            {
                ExtentLength = 6858000,
                ExtentWidth = 9144000
            };

            P.SlideMaster master = pres.CreateSlideMaster();
            P.SlideLayout layout = pres.CreateLayout(master);
            P.Slide slide = pres.CreateSlide(layout);
            DMLS.Theme theme = pres.CreateTheme(master);

            #region Create Master

            {
                master.SlideData = new CommonSlideData()
                {
                    Name = "Slide",
                    ShapeTree = new ShapeTree()
                    {
                        NonVisualGroupShapeProperties = new NonVisualGroupShapeProperties()
                        {
                            NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                            {
                                Id = 1,
                                Name = ""
                            },
                            NonVisualGroupShapeDrawingProperties = new DMLGP.NonVisualGroupShapeDrawingProperties(),
                            NonVisualProperties = new NonVisualProperties()
                        },
                        GroupShapeProperties = new DMLGP.GroupShapeProperties()
                        {
                            Transform2d = new DMLTf.GroupTransform2d()
                            {
                                Offset = new DMLG.Offset() { X = 0, Y = 0 },
                                Extents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 },
                                ChildOffset = new DMLG.Offset() { X = 0, Y = 0 },
                                ChildExtents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 }
                            }
                        }
                    }
                };

                master.ColorSchemeMap = new ColorSchemeMap()
                {
                    Background1 = DMLT.ST_ColorSchemeIndex.Light1,
                    Background2 = DMLT.ST_ColorSchemeIndex.Light2,
                    Text1 = DMLT.ST_ColorSchemeIndex.Dark1,
                    Text2 = DMLT.ST_ColorSchemeIndex.Dark2,
                    Accent1 = DMLT.ST_ColorSchemeIndex.Accent1,
                    Accent2 = DMLT.ST_ColorSchemeIndex.Accent2,
                    Accent3 = DMLT.ST_ColorSchemeIndex.Accent3,
                    Accent4 = DMLT.ST_ColorSchemeIndex.Accent4,
                    Accent5 = DMLT.ST_ColorSchemeIndex.Accent5,
                    Accent6 = DMLT.ST_ColorSchemeIndex.Accent6,
                    Hyperlink = DMLT.ST_ColorSchemeIndex.Hyperlink,
                    FollowedHyperlink = DMLT.ST_ColorSchemeIndex.FollowedHyperlink
                };
            }

            #endregion

            #region Create Layout

            {
                layout.Type = ST_SlideLayout.Text;
                layout.SlideData = new CommonSlideData()
                {
                    ShapeTree = new ShapeTree()
                    {
                        NonVisualGroupShapeProperties = new NonVisualGroupShapeProperties()
                        {
                            NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                            {
                                Id = 1,
                                Name = ""
                            },
                            NonVisualGroupShapeDrawingProperties = new DMLGP.NonVisualGroupShapeDrawingProperties(),
                            NonVisualProperties = new NonVisualProperties()
                        },
                        GroupShapeProperties = new DMLGP.GroupShapeProperties()
                        {
                            Transform2d = new DMLTf.GroupTransform2d()
                            {
                                Offset = new DMLG.Offset() { X = 0, Y = 0 },
                                Extents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 },
                                ChildOffset = new DMLG.Offset() { X = 0, Y = 0 },
                                ChildExtents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 }
                            }
                        }
                    }
                };

                layout.Override = new ColorSchemeMapOverride() { Override = new MasterColorMapping() };
            }

            #endregion

            #region Create Slide

            {
                slide.SlideData = new CommonSlideData()
                {
                    ShapeTree = new ShapeTree()
                    {
                        NonVisualGroupShapeProperties = new NonVisualGroupShapeProperties()
                        {
                            NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                            {
                                Id = 1,
                                Name = ""
                            },
                            NonVisualGroupShapeDrawingProperties = new DMLGP.NonVisualGroupShapeDrawingProperties(),
                            NonVisualProperties = new NonVisualProperties()
                        },
                        GroupShapeProperties = new DMLGP.GroupShapeProperties()
                        {
                            Transform2d = new DMLTf.GroupTransform2d()
                            {
                                Offset = new DMLG.Offset() { X = 0, Y = 0 },
                                Extents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 },
                                ChildOffset = new DMLG.Offset() { X = 0, Y = 0 },
                                ChildExtents = new DMLG.Extent() { ExtentLength = 0, ExtentWidth = 0 }
                            }
                        }
                    }
                };

                Shape shape;
                slide.SlideData.ShapeTree.Shapes.Add(shape = new Shape()
                {
                    NonVisualProperties = new NonVisualShapeProperties()
                    {
                        NonVisualDrawingProperties = new DMLGP.NonVisualProperties()
                        {
                            Id = 2,
                            Name = "Ellipse"
                        },
                        NonVisualShapeDrawingProperties = new DMLGP.NonVisualShapeDrawingProperties(),
                        NonVisualProperties = new NonVisualProperties()
                    },
                    Properties = new DMLGP.ShapeProperties()
                    {
                        Transform2d = new DMLTf.Transform2d()
                        {
                            Offset = new DMLG.Offset() { X = 2483768, Y = 3140968 },
                            Extents = new DMLG.Extent() { ExtentLength = 4464496, ExtentWidth = 1800200 }
                        },
                        Geometry = new DMLTf.PresetGeometry()
                        {
                            ShapeType = DMLT.ST_ShapeType.Ellipse,
                            AdjustValues = new DMLG.AdjustValueList()
                        }
                    },
                    Style = new DMLC.Style()
                    {
                        LineReference = new DMLC.IndexedColor()
                        {
                            Index = "2",
                            ColorElement = new DMLC.SchemeColorModel() 
                            {
                                ColorValue = DMLT.ST_SchemeColorVal.AccentColor1
                            }
                        },
                        FillReference = new DMLC.IndexedColor()
                        {
                            Index = "1",
                            ColorElement = new DMLC.SchemeColorModel()
                            {
                                ColorValue = DMLT.ST_SchemeColorVal.AccentColor1
                            }
                        },
                        EffectReference = new DMLC.IndexedColor()
                        {
                            Index = "0",
                            ColorElement = new DMLC.SchemeColorModel()
                            {
                                ColorValue = DMLT.ST_SchemeColorVal.AccentColor1
                            }
                        },
                        FontReference = new DMLC.IndexedColor()
                        {
                            Index = "minor",
                            ColorElement = new DMLC.SchemeColorModel()
                            {
                                ColorValue = DMLT.ST_SchemeColorVal.LightColor1
                            }
                        }
                    },
                    Body = new DMLTx.Body()
                    {
                        Properties = new DMLTx.Properties.BodyProperties()
                        {
                            ColumnsRightToLeft = false,
                            Anchor = DMLT.ST_TextAnchoringType.Center
                        },
                        Styles = new DMLTx.Properties.TextListStyles()
                    }
                });
                (shape.Style.LineReference.ColorElement as DMLC.SchemeColorModel)
                    .ColorElements.Add(new DMLC.Shade() { Value = 50000 });

                DMLTx.Paragraph p;
                shape.Body.Paragraphs.Add(p = new DMLTx.Paragraph()
                {
                    Properties = new DMLTx.Properties.ParagraphProperties() { Alignment = DMLT.ST_TextAlignType.Center }
                });

                slide.Override = new ColorSchemeMapOverride() { Override = new MasterColorMapping() };
            }

            #endregion

            #region Create Theme

            {
                theme.Elements = new DMLS.ThemeElements()
                {
                    ColorScheme = new DMLC.ColorScheme()
                    {
                        Name = "Sample",
                        Dark1 = new DMLC.Color { ColorElement = new DMLC.SystemColorModel { ColorValue = DMLT.ST_SystemColorVal.WindowTextSystemColor, LastColor = "000000" } },
                        Light1 = new DMLC.Color { ColorElement = new DMLC.SystemColorModel { ColorValue = DMLT.ST_SystemColorVal.WindowSystemColor, LastColor = "FFFFFF" } },
                        Dark2 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "1F497D" } },
                        Light2 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "EEECE1" } },
                        Accent1 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "4F81BD" } },
                        Accent2 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "C0504D" } },
                        Accent3 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "9BBB59" } },
                        Accent4 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "8064A2" } },
                        Accent5 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "4BACC6" } },
                        Accent6 = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "F79646" } },
                        Hyperlink = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "0000FF" } },
                        FollowedHyperlink = new DMLC.Color { ColorElement = new DMLC.RbgColorHexVariant() { Value = "800080" } }
                    },
                    FontScheme = new DMLF.FontScheme()
                    {
                        Name = "Sample",
                        MajorFont = new DMLF.FontElement()
                        {
                            Latin = new DMLF.Font() { Typeface = "Calibri" },
                            EastAsian = new DMLF.Font() { Typeface = "" },
                            ComplexScript = new DMLF.Font() { Typeface = "" }
                        },
                        MinorFont = new DMLF.FontElement()
                        {
                            Latin = new DMLF.Font() { Typeface = "Calibri" },
                            EastAsian = new DMLF.Font() { Typeface = "" },
                            ComplexScript = new DMLF.Font() { Typeface = "" }
                        }
                    },
                    FormatScheme = new DMLS.FormatScheme()
                    {
                        Name = "Sample"
                    }
                };

                #region FillStyles

                theme.Elements.FormatScheme.FillStyles.Add(new DMLV.SolidFill()
                {
                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.StyleColor }
                });

                DMLV.GradientFill gradFill = null;
                theme.Elements.FormatScheme.FillStyles.Add(gradFill = new DMLV.GradientFill()
                {
                    GradientStopList = new DMLV.GradientStopList(),
                    ShadeProperty = new DMLV.LinearGradientFill() { Angle = 16200000, Scaled = true }
                });
                gradFill.GradientStopList.GradientStops.Add(new DMLV.GradientStop()
                {
                    Position = 0,
                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.StyleColor }
                });
                (gradFill.GradientStopList.GradientStops[0].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Tint() { Value = 50000 });
                (gradFill.GradientStopList.GradientStops[0].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.SaturationModulation() { Value = 300000 });
                gradFill.GradientStopList.GradientStops.Add(new DMLV.GradientStop()
                {
                    Position = 35000,
                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.StyleColor }
                });
                (gradFill.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Tint() { Value = 37000 });
                (gradFill.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.SaturationModulation() { Value = 300000 });
                gradFill.GradientStopList.GradientStops.Add(new DMLV.GradientStop()
                {
                    Position = 100000,
                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.StyleColor }
                });
                (gradFill.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Tint() { Value = 15000 });
                (gradFill.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.SaturationModulation() { Value = 350000 });

                DMLV.GradientFill gradFill2 = null;
                theme.Elements.FormatScheme.FillStyles.Add(gradFill2 = new DMLV.GradientFill()
                {
                    GradientStopList = new DMLV.GradientStopList(),
                    ShadeProperty = new DMLV.LinearGradientFill() { Angle = 16200000, Scaled = false }
                });
                gradFill2.GradientStopList.GradientStops.Add(new DMLV.GradientStop()
                {
                    Position = 0,
                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.StyleColor }
                });
                (gradFill2.GradientStopList.GradientStops[0].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Tint() { Value = 51000 });
                (gradFill2.GradientStopList.GradientStops[0].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.SaturationModulation() { Value = 130000 });
                gradFill2.GradientStopList.GradientStops.Add(new DMLV.GradientStop()
                {
                    Position = 35000,
                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.StyleColor }
                });
                (gradFill2.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Tint() { Value = 93000 });
                (gradFill2.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.SaturationModulation() { Value = 130000 });
                gradFill2.GradientStopList.GradientStops.Add(new DMLV.GradientStop()
                {
                    Position = 100000,
                    ColorElement = new DMLC.SchemeColorModel() { ColorValue = DMLT.ST_SchemeColorVal.StyleColor }
                });
                (gradFill2.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Tint() { Value = 94000 });
                (gradFill2.GradientStopList.GradientStops[1].ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.SaturationModulation() { Value = 135000 });

                #endregion

                #region LineStyles

                DMLG.Line line = null;
                theme.Elements.FormatScheme.LineStyles.Add(line = new DMLG.Line()
                {
                    Width = 9525,
                    EndingType = DMLT.ST_LineCap.Flat,
                    Compound = DMLT.ST_CompoundLine.Single,
                    Alignment = DMLT.ST_PenAlignment.Center,
                    LineFillProperty = new DMLV.SolidFill()
                    {
                        ColorElement = new DMLC.SchemeColorModel()
                        {
                            ColorValue = DMLT.ST_SchemeColorVal.StyleColor
                        }
                    },
                    LineDashProperty = new DMLS.PresetDash() { Value = DMLT.ST_PresetLineDashVal.Solid }
                });
                ((line.LineFillProperty as DMLV.SolidFill).ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.Shade() { Value = 95000 });
                ((line.LineFillProperty as DMLV.SolidFill).ColorElement as DMLC.SchemeColorModel).ColorElements.Add(new DMLC.SaturationModulation() { Value = 105000 });

                theme.Elements.FormatScheme.LineStyles.Add(line = new DMLG.Line()
                {
                    Width = 25400,
                    EndingType = DMLT.ST_LineCap.Flat,
                    Compound = DMLT.ST_CompoundLine.Single,
                    Alignment = DMLT.ST_PenAlignment.Center,
                    LineFillProperty = new DMLV.SolidFill()
                    {
                        ColorElement = new DMLC.SchemeColorModel()
                        {
                            ColorValue = DMLT.ST_SchemeColorVal.StyleColor
                        }
                    },
                    LineDashProperty = new DMLS.PresetDash() { Value = DMLT.ST_PresetLineDashVal.Solid }
                });

                theme.Elements.FormatScheme.LineStyles.Add(line = new DMLG.Line()
                {
                    Width = 38100,
                    EndingType = DMLT.ST_LineCap.Flat,
                    Compound = DMLT.ST_CompoundLine.Single,
                    Alignment = DMLT.ST_PenAlignment.Center,
                    LineFillProperty = new DMLV.SolidFill()
                    {
                        ColorElement = new DMLC.SchemeColorModel()
                        {
                            ColorValue = DMLT.ST_SchemeColorVal.StyleColor
                        }
                    },
                    LineDashProperty = new DMLS.PresetDash() { Value = DMLT.ST_PresetLineDashVal.Solid }
                });

                #endregion

                #region EffectStyles

                DMLC.RbgColorHexVariant model = null;
                theme.Elements.FormatScheme.EffectStyles.Add(new DMLE.EffectStyle()
                {
                    EffectProperty = new DMLE.EffectList()
                    {
                        OuterShadow = new DMLS.OuterShadow()
                        {
                            BlurRadius = 40000,
                            Distance = 20000,
                            ColorElement = model = new DMLC.RbgColorHexVariant()
                            {
                                Value = "000000"
                            }
                        }
                    }
                });
                model.ColorElements.Add(new DMLC.Alpha(){Value = 38000});
                theme.Elements.FormatScheme.EffectStyles.Add(new DMLE.EffectStyle()
                {
                    EffectProperty = new DMLE.EffectList()
                    {
                        OuterShadow = new DMLS.OuterShadow()
                        {
                            BlurRadius = 40000,
                            Distance = 23000,
                            ColorElement = model = new DMLC.RbgColorHexVariant()
                            {
                                Value = "000000"
                            }
                        }
                    }
                });
                model.ColorElements.Add(new DMLC.Alpha() { Value = 35000 });

                DMLV.Camera camera = null;
                theme.Elements.FormatScheme.EffectStyles.Add(new DMLE.EffectStyle()
                {
                    EffectProperty = new DMLE.EffectList()
                    {
                        OuterShadow = new DMLS.OuterShadow()
                        {
                            BlurRadius = 40000,
                            Distance = 23000,
                            ColorElement = model = new DMLC.RbgColorHexVariant()
                            {
                                Value = "000000"
                            }
                        }
                    },
                    Scene3dProperties = new DMLV.Scene3dProperties()
                    {
                        Camera = camera = new DMLV.Camera()
                        {
                            PresetCameraType = DMLT.ST_PresetCameraType.OrthographicFront
                        },
                        LightRig = new DMLV.LightRig()
                        {
                            RigPreset = DMLT.ST_LightRigType.ThreePt,
                            Direction = DMLT.ST_LightRigDirection.Top,
                            Rotation = new DMLTf.Rotation() { Latitude = 0, Longitude = 0, Revolution = 1200000 }
                        }
                    },
                    Shape3dPropperties = new DMLV.Shape3dProperties()
                    {
                        TopBevel = new DMLG.Bevel() { Width = 63500, Height = 25400 }
                    }
                });
                camera.Rotations.Add(new DMLTf.Rotation() { Latitude = 0, Longitude = 0, Revolution = 0 });
                model.ColorElements.Add(new DMLC.Alpha() { Value = 35000 });

                #endregion

                #region BackgroundFillStyles

                theme.Elements.FormatScheme.BackgroundFillStyles.Add(new DMLV.NoFill());
                theme.Elements.FormatScheme.BackgroundFillStyles.Add(new DMLV.NoFill());
                theme.Elements.FormatScheme.BackgroundFillStyles.Add(new DMLV.NoFill());

                #endregion
            }

            #endregion

            pres.Save(@"samples\figure.pptx");

            P.Presentation parsed = null;
            if (DocumentManager.Instance.OpenPresentationDocument(@"samples\figure.pptx", out parsed))
            {
                DMLS.Theme parsedTheme = parsed.FindThemes()[0];
                P.SlideLayout parsedLayout = parsed.FindLayoutForSlide(parsed.Slides[0]);
                Console.WriteLine("Sample created and parsed succesfully!");
            }
            Console.ReadLine();
        }
    }
}